package com.example.opengldemo._05_;

import android.content.Context;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceView;

import com.example.opengldemo._01_.Triangle;
import com.example.opengldemo.util.MatrixState;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MySurfaceView extends GLSurfaceView {
    private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
    private MyRenderer mRenderer;
    private float mPreviousY;
    private float mPreviousX;

    private RotateThread thread;

    public MySurfaceView(Context context) {
        super(context);
        this.setEGLContextClientVersion(3);
        mRenderer = new MyRenderer();
        this.setRenderer(mRenderer);
        this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    public boolean onTouchEvent(MotionEvent e) {
        float y = e.getY();
        float x = e.getX();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dy = y - mPreviousY;
                float dx = x - mPreviousX;
                for (SixShape h : mRenderer.sixShapes) {//设置各个六角星绕 x 轴、y 轴旋转的角度 h.yAngle += dx * TOUCH_SCALE_FACTOR;
                    h.mShape += dy * TOUCH_SCALE_FACTOR;
                }
        }
        mPreviousY = y;
        mPreviousX = x; //记录触控点的 x、y 坐标
        return true;
    }


    class MyRenderer implements Renderer {
        SixShape[] sixShapes = new SixShape[6];
        Point_Line_Shape point_line_shape;
        Cube cube;

        @Override
        public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
            GLES30.glClearColor(0.5f, 0.5f, 0.5f, 1f);
            for (int i = 0; i < sixShapes.length; i++) {
                sixShapes[i] = new SixShape(MySurfaceView.this, 0.2f, 0.5f, -0.3f * i);
            }
            point_line_shape = new Point_Line_Shape(MySurfaceView.this);
            cube = new Cube(MySurfaceView.this);
            GLES30.glEnable(GLES30.GL_DEPTH_TEST);
            thread = new RotateThread();
            thread.start();
        }

        @Override
        public void onSurfaceChanged(GL10 gl10, int i, int i1) {
            GLES30.glViewport(0, 0, i, i1);
            float ratio = (float) i / i1;
            MatrixState.setProjectFrustum(-2f, 2f, -1, 1, 2, 10);
            MatrixState.setCamera(0, 0, 3f, 0, 0, 0f, 0f, 1.0f, 0.0f);
            MatrixState.setInitStack();
        }

        @Override
        public void onDrawFrame(GL10 gl10) {
            GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.
                    GL_COLOR_BUFFER_BIT);
            for (SixShape shape : sixShapes
            ) {
                //shape.drawSelf();
            }
            MatrixState.pushMatrix();
            MatrixState.translate(-1f , 0f , 0f );
            cube.drawSelf();

            MatrixState.translate(2f , 0f , 0f );
            cube.drawSelf();
            MatrixState.popMatrix();
        }
    }

    class RotateThread extends Thread {
        public boolean flag = true;

        @Override
        public void run() {
            while (flag) {
             //  mRenderer.point_line_shape.updateDrawState();

               // MatrixState.rotate(0.375f , 0,0, 1);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
